import _ from 'lodash'
import {defineStore} from "pinia";
import useMenuStore from "@/store/useMenuStore";
import useAppStore from "@/store/useAppStore";

interface AuthData {
    jwtToken?: string;
    refreshToken?: string;
    userInfo?: {
        userId: string,
        userName: string,
    }
}

interface UserData {
    name: string
    permissions: string[]
    roles: string[]
    userInfo?: {
        userId: string,
        userName: string,
    }
}

interface SysUserData {
    isEnable: number
    orgId: string
    userCode: string
    userId: string
    userName: string
}

export default defineStore("user", {
    state() {
        const auth: AuthData = undefined
        const user: UserData = undefined
        const sysUser: SysUserData = undefined

        return {
            auth,
            user,
            sysUser,
            loading: false,
        }
    },
    getters: {},
    actions: {
        logout() {
            this.$patch({
                auth: undefined,
                user: undefined,
                sysUser: undefined
            })
        },

        loadFromIndexDb(state) {
            this.$patch(state)
        },

        /**
         * 获取用户数据
         */
        getUserData(): Promise<any> {
            return new Promise((resolve, reject) => {
                this.loading = true
                system.invokeServer('/pages/sys/user/User@userData', {
                    version: null
                }).then(res => {
                    if (res.success) {
                        useMenuStore().$patch({
                            menuList: res.data.menuList,
                            menuVersion: res.data.menuVersion,
                            whList: res.data.wh
                        })
                        if (res.data.wh && _.size(res.data.wh) > 0) {
                            // 设定当前仓库
                            useAppStore().$patch({
                                currentWhName: res.data.wh[0].wh_name,
                                currentWhId: res.data.wh[0].wh_id
                            })
                        }

                        this.sysUser = res.data.sysUser
                        this.user = res.data.user
                        resolve(res.data)
                    } else {
                        reject(res.msg)
                    }

                }).catch(e => {
                    reject(e)

                }).finally(() => {
                    this.loading = false
                })
            })

        },

        /**
         * 登录
         * @param data
         */
        login(data): Promise<any> {
            return new Promise((resolve, reject) => {
                this.loading = true
                system.login(data).then(axiosResponse => {
                    const resp = axiosResponse.data
                    if (resp.success) {
                        const result = resp.data
                        if (result.success) {
                            this.auth = resp.data
                            resolve(resp.data)
                        } else {
                            reject(result.message)
                        }

                    } else {
                        reject(resp.msg)
                    }

                }).catch((e) => {
                    reject(e)

                }).finally(() => {
                    this.loading = false
                })
            })
        }
    }
})
